package com.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marketing.service.AdvertisePositionService;
import com.marketing.service.CustomerIssuesService;
import com.nameless.entity.AdvertisePosition;
import com.nameless.entity.CustomerIssues;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题模块控制器
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/02/28 17:31
 * @Description
 */
@RestController
@Api("问题模块操作接口")
@Slf4j
@RequestMapping("/marketing/api/customerIssues")
public class CustomerIssuesController {

    @Autowired
    private CustomerIssuesService customerIssuesService;

    @PostMapping("{current}/{pageSize}")
    @ApiOperation(value = "分页查询信息并携带查询条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页码，默认为1",example = "1",
                    paramType = "path",dataType = "int",required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认5", example = "5",
                    paramType = "path", dataType = "int", required = false)
    })
    public Result<PageInfo<CustomerIssues>> findPageCustomer(@PathVariable Integer current, @PathVariable Integer pageSize,
                                                         @RequestBody(required = false) CustomerIssues searchObj){
        PageHelper.startPage(current,pageSize);



        LambdaQueryWrapper<CustomerIssues> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(searchObj.getSubmodule()),CustomerIssues::getSubmodule,searchObj.getSubmodule());

        List<CustomerIssues> list = customerIssuesService.list(wrapper);

        PageInfo<CustomerIssues> pageInfo = new PageInfo(list);

        return Result.success(pageInfo);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据问题模块编号信息")
    public Result<CustomerIssues> getOne(@PathVariable Long id){
        CustomerIssues byId = customerIssuesService.getById(id);
        return Result.success("id为："+id+"的问题模块查询成功",byId);
    }

    @DeleteMapping
    @ApiOperation(value = "删除多个问题模块")
    @ApiImplicitParam(name = "idList",value = "需要删除的医院配置id集合")
    public Result<String> delete(@RequestBody List<Integer> idList){
        log.info("idList = {}",idList);
        boolean b = customerIssuesService.removeByIds(idList);
        return b?Result.success("删除成功"):Result.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "添加问题模块信息")
    public Result<String> addCustomer(@RequestBody CustomerIssues customerIssues){
        customerIssues.setAddtime(new Date());
        boolean save = customerIssuesService.save(customerIssues);
        return save?Result.success("添加成功"):Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation(value = "修改问题模块信息")
    public Result<String> editCustomer(@RequestBody CustomerIssues customerIssues){
        boolean save = customerIssuesService.updateById(customerIssues);
        return save?Result.success("修改成功"):Result.error("修改失败");
    }

    @GetMapping("findAll")
    public Result<List<String>> findAll(){
        List<CustomerIssues> list = customerIssuesService.list();
        List<String> collect = list.stream().map(CustomerIssues::getSubmodule).distinct().collect(Collectors.toList());
        return Result.success(collect);
    }
}
